package com.tongji.housediy.view
{
	import com.tongji.housediy.events.commonEvent.MouseEnabledAdminEvent;
	import com.tongji.housediy.events.commonEvent.RoomNameAdminEvent;
	import com.tongji.housediy.events.commonEvent.RulerAdminEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.model.FloorModel;
	import com.tongji.housediy.utils.DragDropManager;
	import com.tongji.housediy.view.components.AbstractDrawingRoom;
	import com.tongji.housediy.view.components.OpeningWall;
	import com.tongji.housediy.view.components.SpecificRoom;
	
	import flash.events.MouseEvent;
	
	import mx.events.DragEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Group;
	import spark.filters.DropShadowFilter;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class ScrollingWallMediator extends Mediator
	{
		[Inject]
		public var view:ScrollingWallContent;
		
		[Inject]
		public var model:FloorModel;
		
		override public function onRegister():void
		{
			view.addEventListener(MouseEvent.MOUSE_DOWN, listMouseDownHandler);
			view.addEventListener(MouseEvent.MOUSE_OVER, listMouseOverHandler);
			view.addEventListener(MouseEvent.MOUSE_OUT, listMouseOutHandler);
			
			view.addEventListener(DragEvent.DRAG_COMPLETE, listDragCompleteHandler);
		}
		
		private function listMouseDownHandler(event:MouseEvent):void
		{
			if (!(event.target is ScrollingWallContent) && (event.target is Group))
			{
				dispatch(new RulerAdminEvent(RulerAdminEvent.HIDE_ALL_RULERS));
				dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
				dispatch(new RoomNameAdminEvent(RoomNameAdminEvent.HIDE_ROOM_NAME));
				
				switch (view.getChildIndex(event.target as Group))
				{
					case 0:
					case 1:
						dispatch(new MouseEnabledAdminEvent(MouseEnabledAdminEvent.MOUSE_ENABLED_CLOSED));
						var roomProxy:AbstractDrawingRoom = new SpecificRoom(view.getChildIndex(event.target as Group) + 21);
						DragDropManager.startDragRoom(view, event, roomProxy);
						break;
					case 2:
						DragDropManager.startDragRoomChild(view, event, "wallFormat");
						break;
					case 3:
						DragDropManager.startDragBorderChild(view, event, "openingFormat", new OpeningWall());
						break;
					case 6:
					case 7:
					case 8:
					case 9:
						DragDropManager.startDragExpandedWall(view, event, "assets/images/" + event.target.id + ".png");
						break;
				}
			}
		}
		
		private function listMouseOverHandler(event:MouseEvent):void
		{
			if (!(event.target is ScrollingWallContent) && (event.target is Group))
			{
				(event.target as Group).filters = [new DropShadowFilter()];
			}
		}
		
		private function listMouseOutHandler(event:MouseEvent):void
		{
			if (!(event.target is ScrollingWallContent) && (event.target is Group))
			{
				(event.target as Group).filters = null;
			}
		}
		
		private function listDragCompleteHandler(event:DragEvent):void
		{
			dispatch(new MouseEnabledAdminEvent(MouseEnabledAdminEvent.MOUSE_ENABLED_OPENED));
		}
	}
}